package com.fh.shop.api.recipient.controller;

import com.fh.shop.api.annotation.NeedLogin;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.common.SystemConst;
import com.fh.shop.api.member.vo.MemberVo;
import com.fh.shop.api.recipient.biz.IRecipientService;
import com.fh.shop.api.recipient.po.Recipient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/recipients")
@Api(tags ="电商前台-收件人信息")
public class RecipientController {
    @Resource(name = "recipientService")
    private IRecipientService recipientService;
    @Resource
    private HttpServletRequest request;

    @PostMapping
    @NeedLogin
    @ApiOperation("添加收件人信息")
    @ApiImplicitParam(name = "x-auth",value = "头信息",dataType = "java.lang.String",required = true,paramType = "header")
    public ServerResponse addRecipient(@RequestBody Recipient recipient){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long id = memberVo.getId();
        recipient.setMemberId(id);
        return recipientService.addRecipient(recipient);
    }


    @GetMapping
    @NeedLogin
    @ApiOperation("查询收件人信息")
    @ApiImplicitParam(name = "x-auth",value = "头信息",dataType = "java.lang.String",required = true,paramType = "header")
    public ServerResponse findInfo(){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long id = memberVo.getId();
        return recipientService.findList(id);
    }

    @PostMapping("{xid}")
    @NeedLogin
    @ApiOperation("更新收件人信息,是否是默认地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "xid",value = "地址id",dataType = "java.lang.Long",example = "0",required = true),
            @ApiImplicitParam(name = "x-auth",value = "头信息",dataType = "java.lang.String",required = true,paramType = "header")
    })
    public ServerResponse updateStatus(@PathVariable("xid") Long id){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return recipientService.updateStatus(id,memberId);
    }



    @DeleteMapping("{id}")
    @NeedLogin
    @ApiOperation("删除收件人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "地址id",dataType = "java.lang.Long",example = "0",required = true),
            @ApiImplicitParam(name = "x-auth",value = "头信息",dataType = "java.lang.String",required = true,paramType = "header")
    })
    public ServerResponse delete(@PathVariable Long id){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return recipientService.deleteRecipient(id,memberId);
    }

    @GetMapping("/find")
    @NeedLogin
    @ApiOperation("回填收件人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "地址id",dataType = "java.lang.Long",example = "0",required = true),
            @ApiImplicitParam(name = "x-auth",value = "头信息",dataType = "java.lang.String",required = true,paramType = "header")
    })
    public ServerResponse find(Long id){
        return recipientService.findRecipient(id);
    }


    @PutMapping
    @NeedLogin
    @ApiOperation("修改收件人信息")
    @ApiImplicitParam(name = "x-auth",value = "头信息",dataType = "java.lang.String",required = true,paramType = "header")
    public ServerResponse update(@RequestBody Recipient recipient){
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
        recipient.setMemberId(memberId);
        return recipientService.updateRecipient(recipient);
    }
}
